<?php
  ini_set('display_errors', 'On');
  error_reporting(E_ALL);

  $persoon = array("id","roepnaam","tussenvoegsel","achternaam","geboortedatum","straat","huisnummer","toevoeging","postcode","plaats","email","telefoon","mobiel1","mobiel2",
    "verzekeraar","polisnummer","huisarts","huisartsTelefoon","gezondheid","dieten","overig","bijzonderheden","geslacht","orderId","barcode", "polsband");

  $presentie = array("ma","di","wo","do","vr","za","zo");
  
  $groep = array("opmerking", "kleur");


  require_once('functions.php');
  
  try {
    
    $db = getConnection();

    if (isset($_POST['change']) && $_POST['change']) {
      $change = $_POST['change'];

      $id = $change[0];
      $colmn = $change[1];
      $value = $change[2];

      if (in_array($colmn, $persoon)) {
        
        if ($colmn == "geboortedatum") {
          if (count(explode('/', $value)) == 3) {
            list($day, $month, $year) = explode('/', $value);
            $value = sprintf("%04d-%02d-%02d ", intval($year), intval($month), intval($day));
          }
        }
        
        $sql = "update persoon set $colmn = '$value' where id='$id' ";
        error_log("save.php: ".$sql."\n", 3, "/tmp/php.log"); 
        $db->exec($sql);
      } else if (in_array($colmn, $presentie)) {
        $sql = "update presentie set '$colmn' = '$value' where id='$id' ";
        error_log("save.php: ".$sql."\n", 3, "/tmp/php.log"); 
        $db->exec($sql);
      } else if (in_array($colmn, $presentie)) {
        $sql = "update groep set '$colmn' = '$value' where id='$id' ";
        error_log("save.php: ".$sql."\n", 3, "/tmp/php.log"); 
        $db->exec($sql);
      } else {
        error_log("save.php: kolom onbekend '$colmn'\n", 3, "/tmp/php.log");    
      }
    }        

    $out = array(
      'result' => 'ok'
    );
    echo json_encode($out);
    
    closeConnection($db);
  } catch (PDOException $e) {
    print 'Exception : ' . $e->getMessage();
  }

?>          
